package org.easydev.base.webbase.exception;

import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
public class BizException extends RuntimeException{

    private final String errorCode;

    private final HttpStatus httpStatus;

    public BizException(String errorCode, String message, HttpStatus httpStatus) {
        super(message);
        this.errorCode = errorCode;
        this.httpStatus = httpStatus;
    }

    public BizException(String errorCode, String message, HttpStatus httpStatus, Throwable cause) {
        super(message, cause);
        this.errorCode = errorCode;
        this.httpStatus = httpStatus;
    }

    public static BizException of(BizExceptionMsg bem) {
        if (bem.getHttpStatus() != null) {
            return of(bem.getErrorCode(), bem.getMessage(), bem.getHttpStatus());
        }
        return of(bem.getErrorCode(), bem.getMessage());
    }

    public static BizException of(BizExceptionMsg bem, Throwable cause) {
        if (bem.getHttpStatus() != null) {
            return of(bem.getErrorCode(), bem.getMessage(), bem.getHttpStatus(), cause);
        }
        return of(bem.getErrorCode(), bem.getMessage(), cause);
    }

    public static BizException of(String errorCode, String message) {
        return new BizException(errorCode, message, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    public static BizException of(String errorCode, String message,HttpStatus httpStatus) {
        return new BizException(errorCode, message, httpStatus);
    }

    public static BizException of(String errorCode, String message, Throwable cause) {
        return new BizException(errorCode, message, HttpStatus.INTERNAL_SERVER_ERROR, cause);
    }

    public static BizException of(String errorCode, String message, HttpStatus httpStatus, Throwable cause) {
        return new BizException(errorCode, message, httpStatus, cause);
    }
}
